#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

create_dir()
{
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p $path; else path=$path`date "+%y%m%d%H%M%S"` && mkdir -p $path; fi
    echo $path
}

buildpath=$(create_dir $1)
installpath=$(create_dir $2)

yum install -y m4 environment-modules systemd-devel
source /etc/profile
module purge

set -e
yum install zlib zlib-devel openssl openssl-devel gcc-gfortran lapack-devel xz-devel curl-devel lzo lzo-devel freetype-devel cmake bzip2-devel ncurses-devel bwa samtools -y 
dep_dir="dependency"
bash $dep_dir/bisheng-compiler-2.1.0.sh $buildpath $installpath
module use $installpath/bisheng-compiler-2.1.0
module load $installpath/bisheng-compiler-2.1.0/bisheng_modulefiles

export CC=`which clang`
export CXX=`which clang++`
export FC=`which flang`

bash $dep_dir/python-2.7.18.sh $buildpath $installpath
module use $installpath/python-2.7.18
module load $installpath/python-2.7.18/python_modulefiles

bash $dep_dir/setuptools-28.8.0.sh $buildpath $installpath

bash $dep_dir/pip-19.3.1.sh $buildpath $installpath

bash $dep_dir/numpy-1.8.0.sh $buildpath $installpath

bash $dep_dir/scipy-0.17.0.sh $buildpath $installpath

bash $dep_dir/matplotlib-1.4.0.sh $installpath

bash $dep_dir/cython-0.21.sh $buildpath $installpath

bash $dep_dir/pysam-0.8.2.sh $buildpath $installpath

bash $dep_dir/python-lzo-1.14.sh $buildpath $installpath

bash $dep_dir/bx-python-0.7.4.sh $buildpath $installpath

bash $dep_dir/numpydoc-0.5.sh $buildpath $installpath

bash $dep_dir/pyBigWig-0.2.1.sh $buildpath $installpath

bash deeptools-2.0.0.sh $buildpath $installpath
module use $installpath/deeptools-2.0.0
module load $installpath/deeptools-2.0.0/deeptools_modulefiles

mkdir -p pkg/deeptools-2.0.0-hpc && cp -r $installpath/* pkg/deeptools-2.0.0-hpc/
cat>"pkg/deeptools-2.0.0-hpc/run.sh"<<EOF
#!/bin/bash
yum install -y m4 environment-modules systemd-devel csh time patch libatomic perl-Data-Dumper \\
autoconf automake libtool numactl binutils valgrind flex wget tar libatomic gcc-c++ git \\
zlib zlib-devel openssl openssl-devel gcc-gfortran lapack-devel xz-devel curl-devel lzo lzo-devel freetype-devel cmake bzip2-devel ncurses-devel bwa samtools
source /etc/profile &> /dev/null 
current_dir=\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" &> /dev/null && pwd)
for f in "\${current_dir}"/*; do
  if [[ -d "\${f}" && -f "\$(ls "\${f}"/*modulefiles 2>&1)" ]]; then
    module use "\${f}" && module load "\${f}"/*modulefiles
  fi
done

for file in \$(ls \$DEEPTOOLS/bin); do
  sed -i "1c#!\$PWD/python-2.7.18/bin/python" \$DEEPTOOLS/bin/\$file
done

echo -e "\033[1;32;1mdeepTools environment initialization completed.\033[0m"

EOF
cd pkg/ && tar zcvf deeptools-2.0.0-hpc.tar.gz deeptools-2.0.0-hpc

rm -rf $buildpath $installpath
